package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.CourseOrder;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.mapper.CourseOrderMapper;
import cn.wolfcode.crm.query.CourseOrderQuery;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.ICourseOrderService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CourseOrderServiceImpl implements ICourseOrderService {

    @Autowired
    private CourseOrderMapper courseOrderMapper;

    @Override
    public void delete(Long id) {
        courseOrderMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void save(CourseOrder courseOrder) {
        //将客户的状态转成证是客户再保存
        courseOrder.getCustomer().setStatus(1);
        //销售人
        //获取当前登录用户
//        Subject subject = SecurityUtils.getSubject();
//        Employee employee = (Employee) subject.getPrincipal();
        //销售时间
        courseOrder.setInputTime(new Date());

        courseOrderMapper.insert(courseOrder);
    }

    @Override
    public CourseOrder get(Long id) {
        return courseOrderMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<CourseOrder> listAll() {
        return courseOrderMapper.selectAll();
    }

    @Override
    public void update(CourseOrder courseOrder) {
        courseOrder.setInputTime(new Date());
        courseOrderMapper.updateByPrimaryKey(courseOrder);
    }

    /**
     *
     * @param qo
     * @return
     */
    @Override
    public PageInfo query(CourseOrderQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());

        //查询结果集
        List<CourseOrder> list = courseOrderMapper.selectForList(qo);
        return new PageInfo(list);
    }

    @Override
    public Long[] selectTitleIds() {
        return courseOrderMapper.selectTitleIds();
    }
}
